package com.example.elderlycare.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.elderlycare.dao.entity.Train;
import com.example.elderlycare.dao.mapper.TrainMapper;
import com.example.elderlycare.service.TrainService;
import com.example.elderlycare.service.entity.QueryListResponseBody;
import com.example.elderlycare.service.entity.TrainItem;
import com.example.elderlycare.utils.FormatDate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TrainServiceImpl extends ServiceImpl<TrainMapper, Train> implements TrainService {
    @Resource
    TrainMapper trainMapper;

    @Override
    public QueryListResponseBody<TrainItem> queryTrainList(Map<String, Object> params) {
        QueryListResponseBody.QueryListResponseBodyBuilder<TrainItem> queryListResponseBodyBuilder = QueryListResponseBody.builder();
        Integer count = trainMapper.queryTrainCount(params);
        queryListResponseBodyBuilder.total(count);
        List<TrainItem> items = new ArrayList<>();
        List<Train> result = trainMapper.queryTrainList(params);
        result.forEach(item -> {
            TrainItem.TrainItemBuilder trainItemBuilder = TrainItem.builder();
            trainItemBuilder.id(item.getId()).
                    startTime(FormatDate.getFormatDate(item.getStartTime())).
                    endTime(FormatDate.getFormatDate(item.getEndTime())).
                    name(item.getTrainName()).
                    address(item.getAddress()).
                    trainTypeId(item.getTrainTypeId()).
                    content(item.getContent()).
                    count(item.getCountNum()).
                    trainer(item.getTrainer()).
                    trainDepartment(item.getTrainDepartment()).
                    trainType(item.getTrainType() == null ? "未定义" : item.getTrainType().getName());
            items.add(trainItemBuilder.build());
        });
        queryListResponseBodyBuilder.items(items);
        return queryListResponseBodyBuilder.build();
    }
}
